package com.example.userdome.service.impl;

import com.example.userdome.mapper.ScoreMapper;
import com.example.userdome.pojo.Score;
import com.example.userdome.service.ScoreService;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
@Service
public class ScoreServiceAddImpl implements ScoreService {
    @Resource
    private ScoreMapper scoreMapper;
    public void  updateScoreById(Integer id, int math, int chinese, int english){
        boolean ifUpdate = true;
        //判断输入成绩是否为合法数据
        if (math>100 || chinese>100 || english>100){
            ifUpdate = false;
        }
        if (math<0 || chinese<0 || english<0){
            ifUpdate = false;
        }

        Score scoreResult = scoreMapper.selectById(id);

        // scoreResult是表里对应id的记录,如果这条记录的数学，语文 英语成绩，与传入的没有变化，那么不更新
        if (scoreResult.getMath() == math && scoreResult.getChinese() == chinese && scoreResult.getEnglish() == english) {
            return;
        }

        //如果response返回的是默认ture值时则进行数据的添加或修改
        if (ifUpdate) {
            Score score = new Score();
            score.setId(id);
            score.setMath(math);
            score.setChinese(chinese);
            score.setEnglish(english);
            scoreMapper.updateById(score);
        }
    }
}
